package com.meryl.dtk;

import com.intellij.ide.IdeView;
import com.intellij.ide.projectView.ProjectView;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;

import javax.swing.tree.TreePath;
import java.io.File;
import java.io.IOException;

public class CreateModuleAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        IdeView ideView = e.getRequiredData(LangDataKeys.IDE_VIEW);
        PsiDirectory directory = ideView.getOrChooseDirectory();
        VirtualFile path = directory.getVirtualFile();

        Application applicationManager = ApplicationManager.getApplication();
        applicationManager.runWriteAction(() -> {
            String moduleName = Messages.showInputDialog("Module Name", "Create Module", Messages.getInformationIcon());
            if (moduleName == null) {
                return;
            }

            VirtualFile moduleVirtualFile = createDir(path, moduleName);

            createDir(moduleVirtualFile, "controller");
            createDir(moduleVirtualFile, "entity");
            createDir(moduleVirtualFile, "service");
            createDir(moduleVirtualFile, "sql");
        });
    }

    private VirtualFile createDir(VirtualFile parentDir, String name) {
        try {
            VirtualFile dir = parentDir.createChildDirectory(this, name);
            return dir;
        } catch (IOException e) {
//            throw new RuntimeException(e);
            return null;
        }
    }

    private VirtualFile createFile(VirtualFile parentDir, String name) {
        try {
            VirtualFile dir = parentDir.createChildData(this, name);
            return dir;
        } catch (IOException e) {
//            throw new RuntimeException(e);
            return null;
        }
    }
}

